home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
EnigmA Amiga Run 1997 February
/
EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso
/
enigma
/
onlypc
/
java
/
samples.bin
/
EmployeeDataManager.java
< prev
next >
Wrap
Text File
|
1996-05-13
|
15KB
|
732 lines
/*
* Title: Employee Data Manager
* Type: Application
* Source: EmployeeDataManager.java
* Description:
* Reads and parses a text file containing employee information; parses,
* and sorts the information based on name, section number or SS# (selected
* by user). The information is then displayed in a scrollable text window.
*/
import java.awt.*;
import java.applet.*;
import java.io.*;
/**
* A simple one-directional external linked list class.
*/
class LinkList extends Object
{
private LinkList therest;
private Object thevalue;
public LinkList(Object obj)
{
therest = null;
thevalue = obj;
}
/**
* Create a new list node; insert it at the head of the list. Return the list.
*/
public LinkList cons(Object obj)
{
LinkList newnode = new LinkList(obj);
newnode.thevalue = thevalue;
newnode.therest = therest;
therest = newnode;
thevalue = obj;
return this;
}
/**
* The object owned by the list node
*/
public Object value()
{
return thevalue;
}
/**
* The remainder of the list
*/
public LinkList rest()
{
return therest;
}
/**
* Swap the values of a and b.
*/
public static void swapValues(LinkList a, LinkList b)
{
Object save = a.thevalue;
a.thevalue = b.thevalue;
b.thevalue = save;
}
}
/**
* An employee name object. Allows creation, validation, and ordering of names.
*/
class Name
{
String val;
final static int MAX_LENGTH = 30;
Name(String v)
{
val = v;
}
String value()
{
return val;
}
public boolean isValid()
{
return (0 < val.length()) && (val.length() <= MAX_LENGTH);
}
public boolean isGreaterThan(Name n)
{
return val.compareTo(n.val) > 0;
}
}
/**
* An employee section object. Allows creation, validation, and ordering of employee sections.
*/
class Section
{
int val;
final static int MAX_LENGTH = 10;
Section(int a)
{
val = a;
}
Section(String a)
throws NumberFormatException
{
System.out.println("Parsing section; a='" + a + "' (" + a.length() + " chars)");
val = Integer.parseInt(a.trim());
}
int value()
{
return val;
}
public boolean isValid()
{
return val > 0;
}
public boolean isGreaterThan(Section a)
{
return val > a.value();
}
}
/**
* An employee social-security number object. Allows creation, validation, and
* ordering of employee SS #'s.
*/
class SSN
{
String val;
final static int MAX_LENGTH = 11;
SSN(String s)
{
val = s;
}
String value()
{
return val;
}
public boolean isValid()
{
if (val.length() > MAX_LENGTH) return false;
if (val.charAt(3) != '-') return false;
if (val.charAt(6) != '-') return false;
try
{
Integer.parseInt(val.substring(0, 2));
Integer.parseInt(val.substring(4, 5));
Integer.parseInt(val.substring(7, 10));
}
catch (NumberFormatException e)
{
return false;
}
return true;
}
public boolean isGreaterThan(SSN s)
{
return val.compareTo(s.val) > 0;
}
}
/**
* Exception signaling an invalid employee data object
*/
class InvalidEmployeeException extends Exception
{
public String toString()
{
return "InvalidEmployeeException";
}
public String getMessage()
{
return "The Employee information is invalid";
}
}
/**
* A completely constructed employee data object.
*/
class Employee
{
public Name name;
public Section section;
public SSN ssn;
final static int NAME_FLD = 0;
final static int AGE_FLD = 1;
final static int SSN_FLD = 2;
public Employee(Name n, Section a, SSN s)
throws InvalidEmployeeException
{
name = n;
if (!name.isValid()) throw new InvalidEmployeeException();
section = a;
if (!section.isValid()) throw new InvalidEmployeeException();
ssn = s;
if (!ssn.isValid()) throw new InvalidEmployeeException();
}
public Employee(String n, String a, String s)
throws InvalidEmployeeException
{
this(new Name(n), new Section(a), new SSN(s));
}
public String toString()
{
String s;
s = (name.value()).trim();
s += " ";
s += (String.valueOf(section.value())).trim();
s += " ";
s += (ssn.value()).trim();
s += "\r\n";
return s;
}
}
/**
* A state-ful list of employee data objects.
*/
class EmployeeList
{
LinkList fir = null;
LinkList cur = null;
public void add(Employee e)
{
if (cur == null) fir = new LinkList(e);
else fir = fir.cons(e);
cur = fir;
}
/**
* Sort the employee list on one of the three fields: name, section, or SS#
*/
public void sort(int fld)
{
// Use a bubble sort, for simplicity
LinkList e = fir;
while (e != null)
{
LinkList ee = e.rest();
while (ee != null)
{
switch (fld)
{
case Employee.NAME_FLD:
if
(
(((Employee)(e.value())).name).
isGreaterThan
((((Employee)(ee.value()))).name)
)
LinkList.swapValues(e, ee);
break;
case Employee.AGE_FLD:
if
(
(((Employee)(e.value())).section).
isGreaterThan
((((Employee)(ee.value()))).section)
)
LinkList.swapValues(e, ee);
break;
case Employee.SSN_FLD:
if
(
(((Employee)(e.value())).ssn).
isGreaterThan
((((Employee)(ee.value()))).ssn)
)
LinkList.swapValues(e, ee);
break;
}
ee = ee.rest();
}
e = e.rest();
}
}
/**
* Return the first employee in the list
*/
public Employee first()
{
cur = fir;
if (cur == null) return null; else return (Employee)(cur.value());
}
/**
* Return the next employee in the list
*/
public Employee next()
{
if (cur == null) return null; else cur = cur.rest();
if (cur == null) return null; else return (Employee)(cur.value());
}
}
/**
* Provide file storage and retrieval of employee data objects.
*/
class EmployeeFileAccessor
{
RandomAccessFile f;
/**
* Open the employee file; create it if it does not exist.
*/
public EmployeeFileAccessor(String fname)
throws IOException
{
f = new RandomAccessFile(fname, "rw");
}
/**
* Set the employee file to the beginning
*/
public void reset()
throws IOException
{
f.seek(0);
}
// The size, in bytes, of an employee record. Records are fixed-size, to
// allow for easy modification.
final static int recsize =
Name.MAX_LENGTH+1 + Section.MAX_LENGTH+1 + SSN.MAX_LENGTH+1 +1;
/**
* Write a new employee to the end of the file
*/
public void addEmployee(Employee e)
throws IOException
{
// Transfer field values to buffer
byte[] b = new byte[recsize];
int pos = 0;
int len = e.name.value().length();
e.name.value().getBytes(0, len, b, pos);
b[pos + len] = '\0';
pos += Name.MAX_LENGTH + 1;
String a = String.valueOf(e.section.value());
len = a.length();
a.getBytes(0, len, b, pos);
b[pos + len] = '\0';
pos += Section.MAX_LENGTH + 1;
len = e.ssn.value().length();
e.ssn.value().getBytes(0, len, b, pos);
b[pos + len] = '\0';
b[recsize-1] = '\n';
// Write buffer to file, as a fixed-size record
f.seek(f.length());
f.write(b);
}
/**
* Fetch the first or next employee from the file.
*/
public Employee getNextEmployee()
throws IOException
{
byte[] b = new byte[recsize];
int nchars;
System.out.println("About to read");
if ((nchars = f.read(b)) <= 0) return null;
System.out.println("Read " + nchars + " chars");
int pos = 0;
String n = new String(b, 0, pos, Name.MAX_LENGTH);
n = n.trim();
System.out.println("Fetched name='" + n + "'");
pos += Name.MAX_LENGTH + 1;
String a = new String(b, 0, pos, Section.MAX_LENGTH);
a = a.trim();
System.out.println("Fetched section=" + a);
pos += Section.MAX_LENGTH + 1;
String s = new String(b, 0, pos, SSN.MAX_LENGTH);
s = s.trim();
System.out.println("Fetched ssn=" + s);
Employee emp;
try
{
emp = new Employee(n, a, s);
}
catch (InvalidEmployeeException e)
{
System.out.println("Invalid employee read from file!");
return null;
}
return emp;
}
/**
* Read the employee file, and build a list of employee objects.
*/
EmployeeList buildEmployeeList()
{
EmployeeList elist = new EmployeeList();
Employee emp;
int noOfEmployees = 0;
try
{
reset();
}
catch (IOException ex)
{
return elist;
}
for (;;)
{
try
{
emp = getNextEmployee();
}
catch (IOException ex)
{
return elist;
}
if (emp == null) break;
noOfEmployees++;
elist.add(emp);
}
System.out.println("noOfEmployees=" + noOfEmployees);
return elist;
}
}
/**
* Demonstration of the employee data manager classes.
* Allows the user to scroll through the list of employees; sort the list on
* either name, section, or SS#; enter information for a new employee; and store
* that information in the file.
*/
public class EmployeeDataManager extends Applet
{
//
// The name of the employee data file...
//
final static String employeeFileName = "employee.dat";
/*Panel addPanel;
Panel sortPanel;
Choice choice;
Button sortbutton;
Button addbutton;
TextArea ta;
TextField nfield;
TextField afield;
TextField sfield;*/
EmployeeFileAccessor efa;
EmployeeDataManager()
{
// Call the superclass constructor (for Applet)
super();
}
/**
* Update the display with the current contents of the employee list.
*/
void updateEmployeeDisplay(EmployeeList elist)
{
Employee emp = elist.first();
String textdata = "";
for (;;)
{
if (emp == null) break;
System.out.println("Appending " + emp.toString() + " to textarea");
textdata += emp.toString();
emp = elist.next();
}
ta.setText(textdata);
repaint();
}
// Every Applet should have the following method:
public void init()
{
//name 30, section 10, ssn 11 == 51
//{{INIT_CONTROLS
setLayout(new BorderLayout());
addPanel=new Panel();
add("North",addPanel);
addPanel.reshape(2,2,618,58);
sortPanel=new Panel();
add("Center",sortPanel);
sortPanel.reshape(3,67,617,120);
label1=new Label("Name:");
addPanel.add(label1);
label1.reshape(6,24,37,18);
nfield=new TextField(22);
addPanel.add(nfield);
nfield.reshape(51,24,187,16);
label2=new Label("Section#:");
addPanel.add(label2);
label2.reshape(250,24,55,16);
afield=new TextField(7);
addPanel.add(afield);
afield.reshape(313,24,63,16);
label3=new Label(" SS#:");
addPanel.add(label3);
label3.reshape(388,24,28,16);
sfield=new TextField(14);
addPanel.add(sfield);
sfield.reshape(422,24,116,16);
addbutton=new Button("Add");
addPanel.add(addbutton);
addbutton.reshape(554,19,47,29);
choice= new Choice();
sortPanel.add(choice);
choice.reshape(21,22,111,25);
choice.addItem("Sort By name");
choice.addItem("Sort By Section");
choice.addItem("Sort By SSN");
sortbutton=new Button("Sort");
sortbutton.setFont(new Font("Dialog",Font.PLAIN,12));
sortPanel.add(sortbutton);
sortbutton.reshape(156,21,80,32);
ta=new TextArea(5,39);
sortPanel.add(ta);
ta.reshape(245,18,328,87);
//}}
// Open the employee file
System.out.println("Opening file");
try
{
efa = new EmployeeFileAccessor(employeeFileName);
System.out.println("File opened");
}
catch (IOException e)
{
System.out.println("Unable to open file " + employeeFileName);
return;
}
// Fetch all employees from file, and display them in the text area
EmployeeList elist = efa.buildEmployeeList();
updateEmployeeDisplay(elist);
layout();
}
// Every Applet should have the following method:
public void start()
{
}
// Every Applet should have the following method:
public void stop()
{
}
public boolean handleEvent(Event e)
{
if ((e.target == sortbutton) && (e.id == Event.ACTION_EVENT))
{
// The SORT button was pressed; perform sort
EmployeeList elist = efa.buildEmployeeList();
elist.sort(choice.getSelectedIndex());
System.out.println("Sort on field " + choice.getSelectedIndex());
// Redisplay the employee data
updateEmployeeDisplay(elist);
}
else if ((e.target == addbutton) && (e.id == Event.ACTION_EVENT))
{
Employee emp;
// The ADD button was pressed; add new employee
System.out.println("Add an employee");
// Create (and validate) an employee object
try
{
emp = new Employee
(nfield.getText(), afield.getText(), sfield.getText());
}
catch (Exception ex1)
{
System.out.println("Invalid employee data");
//showStatus("Invalid employee data; please re-enter");
return true;
}
// Add the employee to the file
try
{
efa.addEmployee(emp);
}
catch (IOException ex2)
{
System.out.println("Unable to add employee to file");
return true;
}
System.out.println("Employee added");
// Redisplay the employee data
EmployeeList elist = efa.buildEmployeeList();
updateEmployeeDisplay(elist);
return true;
}
else if (e.id == Event.WINDOW_DESTROY)
{
System.exit(0);
}
return false;
}
public void paint(Graphics g)
{
}
public static void main(String argv[])
{
// Create a Frame
MainFrame f = new MainFrame("EmployeeDataManager");
// Instantiate the Applet
EmployeeDataManager applet = new EmployeeDataManager();
// Add the Applet to the Frame (Frame's use BorderLayout)
f.add("Center", applet);
// Init and start the Applet
applet.init();
applet.start();
// Resize and show the Frame
f.resize(600, 200);
f.move(50, 50);
//f.layout();
f.show();
}
public String getAppletInfo()
{
return "EmployeeDataManager";
}
//{{DECLARE_CONTROLS
Panel addPanel;
Panel sortPanel;
Label label1;
TextField nfield;
Label label2;
TextField afield;
Label label3;
TextField sfield;
Button addbutton;
Choice choice;
Button sortbutton;
TextArea ta;
//}}
}
class MainFrame extends Frame
{
public MainFrame(String s) {
super(s);
}
// Handle close events by simply exiting
public boolean handleEvent(Event e) {
if (e.id == Event.WINDOW_DESTROY) {
System.exit(0);
}
return false;
}
}